package com.fast.fastenjoy.base.web.tag.model;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.fast.fastenjoy.base.manager.SelectDataProvider;
import com.fast.fastenjoy.base.utils.ApplicationContextUtils;
import com.fast.fastenjoy.base.utils.BeanExUtils;
import com.fast.fastenjoy.base.utils.JSObjectUtils;
import com.fast.fastenjoy.base.utils.StringExUtils;
import com.fast.fastenjoy.base.web.tag.BaseAuthTag;
import com.fast.fastenjoy.base.web.tag.Tags;

/**
 * 下拉列表标签
 * @author chenliaohua
 *
 */
public class SelectTag extends BaseAuthTag{

	private static final long serialVersionUID = 4767738515183594807L;

	private String dataProvider;
	private String options;
	private String showKey;
	private String showValue;
	private String headKey;
	private String headValue;
	private String selectKey;
	
	@Override
	public void doInitParam() {
		
		if(StringExUtils.isEmpty(showKey)) showKey = "id";
		if(StringExUtils.isEmpty(showValue)) showValue = "name";
		if(selectKey == null) selectKey = "";
		
		JSONObject jsonOpt = JSObjectUtils.isJson(options);
		
		if(jsonOpt != null){
			json.putAll(jsonOpt);
		}
	}
	
	private String getDefaultOpt(){
		
		if(headValue == null && headKey == null){
			return StringExUtils.EMPTY;
		}
		
		String html = StringExUtils.$format(getTagTemplate(Tags.SELECT_OPT_TAG), "value", headKey);
		html = StringExUtils.$format(html, "name", headValue);
		
		return html;
	}
	
	@Override
	public void doTag() {
		
		StringBuffer opts = new StringBuffer(getDefaultOpt());
		
		Object bean = ApplicationContextUtils.getBean(dataProvider);
		
		if(bean != null && bean instanceof SelectDataProvider){
			
			List<?> list = ((SelectDataProvider<?>)bean).loadData(json);
			
			if(list != null){
				for(Object obj : list){
					if(obj != null){
						Object valueObj = BeanExUtils.getBeanProperty(obj, showKey);
						Object nameObj = BeanExUtils.getBeanProperty(obj, showValue);
						String selected = "";
						if(StringExUtils.isNotEmpty(selectKey) && selectKey.equals(String.valueOf(valueObj))){
							selected = " selected=\"selected\"";
						}
						String opt = StringExUtils.$format(getTagTemplate(Tags.SELECT_OPT_TAG), "value", String.valueOf(valueObj));
						opt = StringExUtils.$format(opt, "selected", selected);
						opt = StringExUtils.$format(opt, "name", String.valueOf(nameObj));
						opts.append(opt);
					}
				}
			}
		}
		
		String html = StringExUtils.$format(getTagTemplate(Tags.SELECT_TAG), "opts", opts.toString());
		html = getBaseTagHtml(html);
		
		writeHtml(html);
	}

	public String getDataProvider() {
		return dataProvider;
	}

	public void setDataProvider(String dataProvider) {
		this.dataProvider = dataProvider;
	}

	public String getOptions() {
		return options;
	}

	public void setOptions(String options) {
		this.options = options;
	}

	public String getShowKey() {
		return showKey;
	}

	public void setShowKey(String showKey) {
		this.showKey = showKey;
	}

	public String getShowValue() {
		return showValue;
	}

	public void setShowValue(String showValue) {
		this.showValue = showValue;
	}

	public String getHeadKey() {
		return headKey;
	}

	public void setHeadKey(String headKey) {
		this.headKey = headKey;
	}

	public String getHeadValue() {
		return headValue;
	}

	public void setHeadValue(String headValue) {
		this.headValue = headValue;
	}

	public String getSelectKey() {
		return selectKey;
	}

	public void setSelectKey(String selectKey) {
		this.selectKey = selectKey;
	}

}
